<template>
    <el-select v-model="selectedVal" :clearable="clearable" filterable class="filter-item" placeholder="选择物品" style="width: 150px;">
        <el-option label="未选择" :value="0" v-if="form"></el-option>
        <el-option
                v-for="(item, index) in items"
                :key="index"
                :label="item.id + ' ) ' + item.name"
                :value="item.id">
        </el-option>
    </el-select>
</template>

<script>
    export default {
        props: {
            value: {},
            form: {
                type: Boolean,
                default: false,
            },
            clearable: {
                type: Boolean,
                default: true,
            },
            type: {
                type: Array,
                default: null,
            },
            ids: {
                type: Array,
                default: null,
            }
        },
        data () {
            return {
                selectedVal: '',
            };
        },
        computed: {
            items () {
                let items = this.$helper.deepClone(this.$store.state.item.all);
                // 指定类型
                if (this.type) {
                    items = items.filter(v => {
                        return this.type.indexOf(parseInt(v.type)) > -1;
                    });
                }
                if (this.ids) {
                    items = items.filter(v => {
                        return this.ids.includes(v.id);
                    });
                }
                return items;
            },
        },
        watch: {
            value() {
                this.syncForm();
            },
            selectedVal() {
                this.$emit('input', this.selectedVal);
            }
        },
        methods: {
            getItems() {
                if (!this.items.length) this.$store.dispatch('item/All', 'item');
            },
            syncForm() {
                this.selectedVal = this.value;
            },
        },
        created() {
            this.getItems();
            this.syncForm();
        }
    }
</script>

<style scoped>

</style>
